{% extends "base.html" %}

{% block title %}{{ post.title }} - {{ super() }}{% endblock %}

{% block content %}
<div class="container">
    <nav aria-label="breadcrumb" class="mt-3">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ url_for('community.index') }}">社区交流</a></li>
            <li class="breadcrumb-item active">{{ post.title }}</li>
        </ol>
    </nav>

    <div class="card">
        <div class="card-body">
            <h1 class="card-title">{{ post.title }}</h1>
            
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div class="text-muted">
                    <span><i class="fas fa-user"></i> {{ post.user.username }}</span>
                    <span class="ms-3"><i class="fas fa-clock"></i> {{ post.created_at.strftime('%Y-%m-%d %H:%M') }}</span>
                </div>
                <div>
                    <span class="me-3"><i class="fas fa-eye"></i> {{ post.views }}</span>
                    <span><i class="fas fa-comments"></i> {{ post.comments|length }}</span>
                </div>
            </div>

            <div class="card-text mb-4">
                {{ post.content|safe }}
            </div>
        </div>
    </div>

    <div class="card mt-4">
        <div class="card-header">
            <h5 class="mb-0">评论 ({{ comments|length }})</h5>
        </div>
        
        {% if current_user.is_authenticated %}
        <div class="card-body">
            <form method="post" action="{{ url_for('community.comment', id=post.id) }}">
                {{ form.csrf_token }}
                <div class="mb-3">
                    {{ form.content(class="form-control", rows="3", placeholder="写下你的评论...") }}
                    {% if form.content.errors %}
                    <div class="invalid-feedback d-block">
                        {% for error in form.content.errors %}
                        <span>{{ error }}</span>
                        {% endfor %}
                    </div>
                    {% endif %}
                </div>
                <button type="submit" class="btn btn-primary">发表评论</button>
            </form>
        </div>
        {% endif %}

        <div class="list-group list-group-flush">
            {% for comment in comments %}
            <div class="list-group-item">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <div>
                        <span class="fw-bold">{{ comment.user.username }}</span>
                        <small class="text-muted ms-2">{{ comment.created_at.strftime('%Y-%m-%d %H:%M') }}</small>
                    </div>
                    {% if current_user.is_authenticated and current_user.id == comment.user_id %}
                    <form method="post" action="{{ url_for('community.delete_comment', id=comment.id) }}" class="d-inline">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要删除这条评论吗？')">
                            删除
                        </button>
                    </form>
                    {% endif %}
                </div>
                <div>{{ comment.content }}</div>
            </div>
            {% else %}
            <div class="list-group-item text-center text-muted py-4">
                暂无评论
            </div>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %} 